import 'package:dmz/base/controller/base_controller.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

///空布局
Widget createEmptyWidget(BaseController controller) {
  return Container(
      alignment: Alignment.center,
      color: Colors.black,
      child: GestureDetector(
          onTap: () {
            // controller.showLoading();
            controller.loadNet();
          },
          child: Image.asset(
            "assets/images/home/main/position_image.png",
            height: 320.w,
            width: 320.w,
          ).marginOnly(bottom: 500.sp)));
}

///创建AppBar
AppBar createAppBar(
    String titleString, bool showBackButton, List<Widget>? actionWidget,
    {Widget? titleWidget}) {
  return AppBar(
      title: titleWidget ?? titleView(titleString),
      centerTitle: true,
      backgroundColor: Colors.transparent,
      iconTheme: const IconThemeData(color: Colors.white),
      elevation: 0,
      systemOverlayStyle: systemOverLayoutStyle(),
      leading: leadingButton(),
      actions: actionWidget);
}

Widget titleView(String titleString) {
  return Text(titleString,
      style: TextStyle(
          color: Colors.white, fontSize: 40.sp, fontWeight: FontWeight.bold));
}

///回退按钮
Widget leadingButton() {
  return IconButton(
    icon: Container(
        color: Colors.transparent,
        padding: EdgeInsets.only(left: 20.sp),
        alignment: Alignment.centerLeft,
        width: 120.sp,
        height: 120.sp,
        child: Image.asset('assets/images/login/back_icon.png',
            width: 34.sp, height: 38.sp)),
    onPressed: () async {
      // onBackPressed();
      var canPop = navigator?.canPop();
      if (canPop != null && canPop) {
        Get.back();
      } else {
        SystemNavigator.pop();
      }
    },
  );
}

Widget onlyBakcWidget({bool isBlack = false}) {
  return GestureDetector(
      onTap: () {
        var canPop = navigator?.canPop();
        if (canPop != null && canPop) {
          Get.back();
        } else {
          SystemNavigator.pop();
        }
      },
      child: Container(
          color: Colors.transparent,
          padding: EdgeInsets.only(left: 20.sp),
          alignment: Alignment.centerLeft,
          width: 120.sp,
          height: 120.sp,
          child: Image.asset('assets/images/login/back_icon.png',
              width: 34.sp,
              height: 38.sp,
              color: isBlack ? const Color(0xFF434650) : null)));
}

Future<void> pop() async {
  await SystemChannels.platform.invokeMethod('SystemNavigator.pop');
}

///状态栏颜色设置
SystemUiOverlayStyle systemOverLayoutStyle() {
  return const SystemUiOverlayStyle(
      statusBarColor: Colors.transparent,
      systemNavigationBarColor: Color(0x00EA4C43),
      systemNavigationBarIconBrightness: Brightness.light,
      statusBarIconBrightness: Brightness.light);
}

///异常布局
Widget createErroWidget(BaseController controller, String? error) {
  return Material(
      child: Center(
          widthFactor: double.infinity,
          child: GestureDetector(
            onTap: () {
              // controller.showLoading();
              controller.loadNet();
            },
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Image.asset(
                  "assets/images/common_empty_img.png",
                  height: 320.w,
                  width: 320.w,
                ),
                SizedBox(width: 30.w),
                Text(
                  error ?? "页面加载异常",
                  style: TextStyle(fontSize: 32.sp, color: Color(0x999999)),
                ),
                SizedBox(width: 20.w),
                Text(
                  "点我重试",
                  style: TextStyle(fontSize: 25.sp, color: Color(0x999999)),
                )
              ],
            ),
          )));
}

Widget createCustomHoldreWidget(String? error, BaseController controller) {
  return Material(
      child: Center(
          widthFactor: double.infinity,
          child: GestureDetector(
            onTap: () {
              // controller.showLoading();
              controller.loadNet();
            },
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Image.asset(
                  "assets/images/common_empty_img.png",
                  height: 320.w,
                  width: 320.w,
                ),
                SizedBox(width: 30.w),
                Text(
                  error ?? "页面加载异常",
                  style: TextStyle(fontSize: 32.sp, color: Color(0x999999)),
                ),
                SizedBox(width: 20.w),
                Text(
                  "点我重试",
                  style: TextStyle(fontSize: 25.sp, color: Color(0x999999)),
                )
              ],
            ),
          )));
}
